/***清空购物车***/
var emptyCart = function(){
    $('.grid').css('display','none');
	$('#orderbox').css('display','none');
}

/*****移入收藏夹(type=1)、删除(type=2)****/
var addToCustWish = function(id,type){
    var _id=id+'_course';
	$('#'+_id).remove();
	if($('table.grid tr').length==2){
		$('.grid').css('display','none');
	    $('#orderbox').css('display','none');
	}
}

/****保存收货人信息****/
      $(function(){
			$('#truename,#province,#company,#mobile,#certificate,#number,#address,#title,#invoice').bind({
					blur:function(){
						FormValidation(this);
					},
					focus:function(){
						var _tipsbox=$(this).nextAll('label.tips');
						_tipsbox.removeClass('moren');
						if(_tipsbox.hasClass('eer')){
							
						}else{
							_tipsbox.addClass('default');
						}
					}
				});
				
				$('.btn-public').click(function(){
				    FormValidation($('#title')[0]);
					FormValidation($('#invoice')[0]);
					var _err=$('.invoice .err_info');
					if(_err.length!=0){
						return false;
					}else{
						return true;
					}
				});
				
				$('#reg_submit_btn').click(function() {
					FormValidation($('#province')[0]);
					FormValidation($('#truename')[0]);
					FormValidation($('#mobile')[0]);
					FormValidation($('#number')[0]);
					FormValidation($('#address')[0]);
					var _err=$('.add-con .err_info');
					if(_err.length!=0){
						return false;
					}else{
						if($('.pay-menu').length!=0){
							$('#details-form').css('display','none');
							$('#info-grid').css('display','block');
							/**发请求拿到列表地址**/
							var address='';
							for(var i =0;i<2;i++){
								if(i==0){
									address =address+'<input name="a" type="radio" checked="checked" value="1">'+
										  '<span id="'+i+'_truename">朱华刚</span>， <span id="'+i+'_province">北京</span>,<span id="'+i+'_city">北京市</span>，'+
										  '<span id="'+i+'_county">海淀区</span>，<span id="'+i+'_address"> 海淀区中关村大街32号</span>，<span id="'+i+'_number">10086</span>，'+
										  '<span id="'+i+'_mobile">15298654784</span>'+
										  '<a href="javascript:updateAddress(\''+i+'\');">修改</a><br/>';
								}else{
									address =address+'<input type="radio" name="a" value="1">'+
										  '<span id="'+i+'_truename">朱华刚</span>， <span id="'+i+'_province">北京</span>,<span id="'+i+'_city">北京市</span>，'+
										  '<span id="'+i+'_county">海淀区</span>，<span id="'+i+'_address"> 海淀区中关村大街32号</span>，<span id="'+i+'_number">10086</span>，'+
										  '<span id="'+i+'_mobile">15298654784</span>'+
										  '<a href="javascript:updateAddress(\''+i+'\');">修改</a><br/>';
								}
							}
							$('#address-list').html(address);
						}
						
						return true;
					}
					
				});
				$('#new-address').click(function(){
				    $('#details-form').css('display','block');
				    $('#info-grid').css('display','none');
				});
				/**支付方式tab切换*/
				var tabLi = $('.rank-menu > li');
				var tabLiA = $('.rank-menu > li a');
				var tabContent = $('.pay-menu > li');
				tabLi.live('click', function(){
					for(var i=0;i<tabLi.length;i++){
						if(i==$(this).index()){
							$(tabLiA).eq(i).addClass('current'); 
							$(tabContent).eq(i).removeClass('hover-content');
						}else{
							if($(tabLiA).eq(i).hasClass('current')){
							   $(tabLiA).eq(i).removeClass('current');
							   $(tabContent).eq(i).addClass('hover-content');
							}
						}
					}
				});
				/**支付方式tab切换*/
			});
		    var changeShow = function(id){
			    $('#'+id).toggle();
				if($('#'+id+'-tip').hasClass('title_border')){
					$('#'+id+'-tip').removeClass('title_border');
				}else{
				   $('#'+id+'-tip').addClass('title_border');
				}
				
			}
			/*确认方法*/
			var updateAddress = function(id){
				var truename = $('#'+id+'_truename').text();
				var province = $('#'+id+'_province').text();
				var city = $('#'+id+'_city').text();
				var county = $('#'+id+'_county').text();
				var address = $('#'+id+'_address').text();
				var number = $('#'+id+'_number').text();
				var mobile = $('#'+id+'_mobile').text();
			    $('#truename').attr('value',truename);
				$('#province').attr('value',province);
				$('#city').attr('value',city);
				$('#county').attr('value',county);
				$('#address').attr('value',address);
				$('#number').attr('value',number);
				$('#mobile').attr('value',mobile);
				$('#details-form').css('display','block');
			    $('#info-grid').css('display','none');
			}
			
			function setState (obj,cls,text){
				var _tips=$(obj);
				var _defalut_text=_tips.text();
				_tips.find('p').remove();
				if(cls=='eer'){
					_tips.append('<p class="err_info">'+text+'</p>');
				}
			};
			
			function checkForm(obj,func){
				var rules={
					trueName	: '',
					province    : '',
					address     : '',
					mobile		: "^1[3|5|8][0-9][0-9]{8}$",
					isCN		: '^[chr(0xa1)-chr(0xff)]+$',
					numeric		: "^[0-9]+$",
					loginname	: "^[a-z][a-z0-9-_.]+$",
					phone		: "^([0-9]{0}|[0-9]{3,4})[-]{0,1}[0-9]{7,8}$",
					idcard		: "^([0-9]{15}|[0-9]{18})$",
					zip			: "^[0-9]{6}$",
					title       :'',
					invoice     :''
				};
				var arg=arguments;
				if(arg.length>0){
					var reg	= new RegExp(rules[obj.type] , "gi");
					var result	= reg.test(obj.value)?true:false;
					if(arg.length>1&&typeof(arg[1])=='function'){ 
						return func(result);
					}else{
						return result;
					}
				}else{
					alert('请设置您要检查的内容');
				}
			};
			
			function checkDate(date)
			{
				return true;
			}
			function FormValidation(n){
				var rules={
					truename:function(){
					    var _name_err=$('#name_err')[0];
						if(n.value.length>1){ 
							checkForm({'value':n.value,'type':'isCN'},function(t){
								if(t){
									setState(_name_err,'eer','请输入真实姓名');
								}else{
									setState(_name_err,'suc','');
								};
							});
						}else{
							setState(_name_err,'eer','请输入真实姓名');
						}
					},
					title:function(){
					    var _title_err=$('#title_err')[0];
						if(n.value.length>1){ 
							checkForm({'value':n.value,'type':'isCN'},function(t){
								if(t){
									setState(_title_err,'eer','请输入真实姓名');
								}else{
									setState(_title_err,'suc','');
								};
							});
						}else{
							setState(_title_err,'eer','请输入真实姓名');
						}
					},
					province:function(){
						var _province_err = $('#province_err')[0];
						var _province=$('#province').val();
						if(_province!=''){ 
							setState(_province_err,'suc','');
						}else{
							setState(_province_err,'eer','请输入联系地址');
						}
					},
					invoice:function(){
						var _invoice_err = $('#invoice_err')[0];
						var _invoice=$('#invoice').val();
						if(_invoice!=''){ 
							setState(_invoice_err,'suc','');
						}else{
							setState(_invoice_err,'eer','请输入发票类型');
						}
					},
					company:function(){
						if(n.value.length>1&&n.value.length<51){ 
							checkForm({'value':n.value,'type':'isCN'},function(t){
								if(t){
									setState($('#company')[0],'eer','请输入真实工作单位 ');
								}else{
									setState($('#company')[0],'suc','');
								};
							});
						}else{
							setState($('#company')[0],'eer','请输入真实工作单位 ');
						}
					},
					number:function(){
						var _number_err = $('#number_err')[0];
						if(n.value.length>1&&n.value.length<51){ 
							setState(_number_err,'suc','');
						}else{
							setState(_number_err,'eer','请输入邮政编码');
						}
					},
					mobile:function(){
						var _mobile_err = $('#mobile_err')[0];
						checkForm({'value':n.value,'type':'mobile'},function(t){
						var _box=$('#mobile')[0];
							if(t){
								setState(_mobile_err,'suc','');
							}else{
								setState(_mobile_err,'eer','填写的手机号码错误');
							}
						});
					},
				   address:function(){
					    var _address_err = $('#address_err')[0];
						var _address=$('#address').val();
						if(_address!=''){ 
							setState(_address_err,'suc','');
						}else{
							setState(_address_err,'eer','请输入详细地址');
						}
				   }
				};
				rules[n.id]();
			};
            

/****保存收货人信息****/

var myTestMethod = {
	changePictureMethod:function(){
		var courseNum = $('.course-item').size();
		for(var i =0;i<courseNum;i++){
		    $('.course-item').eq(i).addClass('course-item_'+i);
			$('.course-num').eq(i).addClass('course-num_'+i);
			$('.course-item_'+i).jMarquee({
				visible:4,
				step:4,
				direction:"left",
				width:229,
				className:'.course-item_'+i,
				childrenClassName:'.recommend'
			});
		}
	}
}
$(document).ready(function(){
   myTestMethod.changePictureMethod();
});